{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串的定义，单引号和双引号没有区别，三引号可以定义多行的字符串\n",
    "a = \"hello\"\n",
    "b = 'world'\n",
    "c = '''\n",
    "hello\n",
    "world\n",
    "'''\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串可以进行乘法和加法\n",
    "a = \"hello\"\n",
    "b = \"world\"\n",
    "print(a + b)\n",
    "print(\"*\" * 50)\n",
    "\n",
    "import time\n",
    "for i in range(1,101):\n",
    "    print(f'\\r[{i}%]{\"*\" * i}', end='')\n",
    "    # time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串索引和下标，可以对字符串进行切片\n",
    "a = \"黄河之水天上来\"\n",
    "#    0 1 2 3 4 5 6\n",
    "#    -7 -6 -5 -4 -3 -2 -1\n",
    "print(a[0])\n",
    "print(a[-3])\n",
    "print(a[0:4])\n",
    "print(a[:]) # 从开头，到结尾，不写表示最开始或结束\n",
    "print(a[::2])\n",
    "print(a[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# python 中的转义字符\n",
    "print(\"a\\nb\")\n",
    "print(\"a\\tb\")\n",
    "print(\"a\\rb\")\n",
    "print(\"a\\bb\")\n",
    "print(\"a\\ab\")\n",
    "\n",
    "# 字符串不允许任何转义字符, 原始字符串\n",
    "print(r\"a\\nb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 字符串的格式化输出\n",
    "name = \"张三\"\n",
    "age = 18.35\n",
    "# 1、.format\n",
    "str1 = \"你好，我叫{a},我今年{b}岁了\".format(b=age,a=name)\n",
    "print(str1)\n",
    "# 2、%()\n",
    "str2 = \"你好，我叫%s,我今年%10.2f岁了\" %(name, age)\n",
    "print(str2)\n",
    "# 3、f\"\"\n",
    "str3 = f\"你好，我叫{name},我今年{age}岁了\"\n",
    "print(str3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串的内建函数\n",
    "> 使用这些方法，可以更有效快速的处理字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "str1='''\n",
    "先帝创业未半而中道崩殂，今天下三分，益州疲弊，此诚危急存亡之秋也。然侍卫之臣不懈于内，忠志之士忘身于外者，盖追先帝之殊遇，欲报之于陛下也。诚宜开张圣听，以光先帝遗德，恢弘志士之气，不宜妄自菲薄，引喻失义，以塞忠谏之路也。\n",
    "\n",
    "宫中府中，俱为一体，陟罚臧否，不宜异同。若有作奸犯科及为忠善者，宜付有司论其刑赏，以昭陛下平明之理，不宜偏私，使内外异法也。\n",
    "\n",
    "侍中、侍郎郭攸之、费祎、董允等，此皆良实，志虑忠纯，是以先帝简拔以遗陛下。愚以为宫中之事，事无大小，悉以咨之，然后施行，必能裨补阙漏，有所广益。\n",
    "\n",
    "将军向宠，性行淑均，晓畅军事，试用于昔日，先帝称之曰能，是以众议举宠为督。愚以为营中之事，悉以咨之，必能使行阵和睦，优劣得所。\n",
    "\n",
    "亲贤臣，远小人，此先汉所以兴隆也；亲小人，远贤臣，此后汉所以倾颓也。先帝在时，每与臣论此事，未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军，此悉贞良死节之臣，愿陛下亲之信之，则汉室之隆，可计日而待也。\n",
    "\n",
    "臣本布衣，躬耕于南阳，苟全性命于乱世，不求闻达于诸侯。先帝不以臣卑鄙，猥自枉屈，三顾臣于草庐之中，咨臣以当世之事，由是感激，遂许先帝以驱驰。后值倾覆，受任于败军之际，奉命于危难之间，尔来二十有一年矣。\n",
    "\n",
    "先帝知臣谨慎，故临崩寄臣以大事也。受命以来，夙夜忧叹，恐托付不效，以伤先帝之明，故五月渡泸，深入不毛。今南方已定，兵甲已足，当奖率三军，北定中原，庶竭驽钝，攘除奸凶，兴复汉室，还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益，进尽忠言，则攸之、祎、允之任也。\n",
    "\n",
    "愿陛下托臣以讨贼兴复之效，不效，则治臣之罪，以告先帝之灵。若无兴德之言，则责攸之、祎、允等之慢，以彰其咎；陛下亦宜自谋，以咨诹善道，察纳雅言，深追先帝遗诏，臣不胜受恩感激。\n",
    "\n",
    "今当远离，临表涕零，不知所言。\n",
    "'''\n",
    "# 查看某个字符在整个字符串中出现的次数\n",
    "# print(str1.count(\"先帝\"))\n",
    "\n",
    "# 字符编码和解码\n",
    "# text = str1.encode()\n",
    "# print(text.decode())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str2 = \"newhello@163.com\"\n",
    "# startswith 以xx开头  endswith 以xx结尾\n",
    "if str2.endswith(\"@163.com\"):\n",
    "    print(\"163\")\n",
    "elif str2.endswith(\"@qq.com\"):\n",
    "    print(\"QQ\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(str1.find(\"卑鄙小人\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 判断字符串中是否是阿拉伯数字\n",
    "num = input(\"请输入一个数字：\")\n",
    "print(num.isdigit())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 判断字符串中是否是数字字符\n",
    "print(\"ⅠⅡⅢⅣ\".isnumeric())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用某个符号将列表元素整合\n",
    "a = [\"你\",\"好\",\"我\",\"是\",\"小\",\"名\"]\n",
    "print(\"\".join(a))\n",
    "\n",
    "# 使用split将某个字符以xx分隔符分割\n",
    "a = \"你,好,我,是,小,明\"\n",
    "print(a.split(\",\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str1 = \"父母生死杀你我\"\n",
    "str2 = \"*\" * len(str1) # 用*组成的和str1一样长的字符串\n",
    "table = \"\".maketrans(str1,str2)\n",
    "\n",
    "result = input(\"队友> \")\n",
    "print(result.translate(table))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = \"#hello###\"\n",
    "print(a.strip(\"#\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
